package com.ruix.app.mapper;

import com.ruix.app.model.Comment;
import com.ruix.app.model.PostsCard;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by hadexs on 2017/5/1.
 */

public class PostCommentsMapper implements ApiMapper<ArrayList<Comment>> {

    @Override
    public ArrayList<Comment> transform(JSONObject response) {
        ArrayList posts = new ArrayList();
        JSONObject data = response.optJSONObject("data");
        if (data != null) {
            JSONObject post = data.optJSONObject("post");
            if (post != null) {
                JSONArray commentList = post.optJSONArray("comments");
                if (commentList != null) {
                    for (int i = 0; i < commentList.length(); i++) {
                        JSONObject object = commentList.optJSONObject(i);
                        Comment comment = new Comment().parse(object);
                        posts.add(comment);
                    }
                }
            }
        }
        return posts;
    }
}